package com.wxxymaker.controller;

import com.blade.ioc.annotation.Inject;
import com.blade.kit.EncryptKit;
import com.blade.mvc.WebContext;
import com.blade.mvc.annotation.*;
import com.blade.mvc.http.Request;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.hook.annotation.AdminIgnore;
import com.wxxymaker.hook.annotation.AdminRequire;
import com.wxxymaker.model.entity.Admin;
import com.wxxymaker.model.entity.Article;
import com.wxxymaker.service.*;
import com.wxxymaker.utils.Base64Util;
import com.wxxymaker.utils.Const;
import com.wxxymaker.utils.TimeUtils;
import io.github.biezhi.anima.page.Page;

@Path("/admin")
@AdminRequire
public class AdminController {

    @Inject
    private AdminService adminService;

    @Inject
    private ArticleService articleService;

    @Inject
    private MemberService memberService;

    @Inject
    private EmailService emailService;

    @Inject
    private VisitorService visitorService;

    @GetRoute({"/", "/admin"})
    public String admin(Request request) {
        request.attribute("articleNum", articleService.count(null, null))
                .attribute("memberNum", memberService.count())
                .attribute("emailNum", emailService.count(null))
                .attribute("viewNum", visitorService.count())
                .attribute("todayVs", visitorService.count(TimeUtils.nowDate()));
        return this.render("admin");
    }

    @GetRoute("/publish")
    public String publish(Request request, @Param(defaultValue = "") Integer id){
//        Article one = new Article();
//        if (id != null && id > 0) {
//            one = articleService.findOne(id);
//        }
        request.attribute("id", id);
        return this.render("publish");
    }

    @GetRoute("/articles")
    public String articles() {
        return render("articles");
    }

    @GetRoute("/member")
    public String member(Request request) {
        request.attribute("pages", new Page());
        return render("member");
    }

    @GetRoute("/email")
    public String email(Request request) {
        request.attribute("pages", new Page());
        return render("email");
    }

    @GetRoute("/setting")
    public String setting() {
        return render("setting");
    }

    @AdminIgnore
    @GetRoute("/login")
    public String login(Request request, @Param(defaultValue = "") String prev) {
        request.attribute("prev", Base64Util.decode(prev));
        return this.render("login");
    }

    @AdminIgnore
    @PostRoute("/login")@JSON
    public RestResponse<?> login(@Param Admin admin) {
        Admin check = adminService.check(admin.getName(), EncryptKit.md5(admin.getPass().getBytes()));
        if (check != null) {
            if (admin.isRemember()) {
                WebContext.response().cookie(Const.CookieKey, check.getMd5Np(), Const.OneDay * 7);
            }

            check.setPass(EncryptKit.md5(check.getPass())); // 保证保存在session和cookie中的密码都是加密的
            WebContext.request().session().attribute(Const.SessionKey, check);
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    @PostRoute("/logout")@JSON
    public RestResponse<?> logout() {
        WebContext.response().cookie(Const.CookieKey, "", 0);
        WebContext.request().session().removeAttribute(Const.SessionKey);
        return RestResponse.ok();
    }

    @PostRoute("/change")@JSON
    public RestResponse<?> change(@Param Admin admin) {
        if (adminService.change(admin)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    private String render(String path) {
        return "admin/" + path;
    }
}
